<template>
	<view :style="viewColor">
		<view class='integral-details'>
			<view class='header'>
				<view class='currentScore'>当前绿色积分 <text @click="showProtocol=true" class="iconfont wenhao">?</text></view>
				<view class="scoreNum">{{userInfo.reward_points ? userInfo.reward_points : 0}}</view>
				<!-- <view class='line'></view> -->
				<view class='nav acea-row'>
					<view class='item'>
						<view class='num'>{{userInfo.total_gain_reward_points || 0}}</view>
						<view>消费积分</view>
					</view>
					<view class='item'>
						<view class='num'>{{userInfo.total_share_reward_points || 0}}</view>
						<view>分享积分</view>
					</view>
					<view class='item'>
						<view class='num'>{{userInfo.lock_reward_points || 0}}</view>
						<view>待结算</view>
					</view>
				</view>
			</view>
			<view class='wrapper'>
				<view class='nav acea-row'>
					<view class='item acea-row row-center-wrapper' :class='current==index?"on":""' v-for="(item,index) in navList" :key='index'
					 @click='nav(index)'><text class='iconfont' :class="item.icon"></text>{{item.name}}</view>
				</view>
				<view class='list' :hidden='current != 0' >
					<view class='tip' v-if="userInfo.clear && userInfo.clear.status && userInfo.clear.nextClearReward > 0"><text class='iconfont icon-shuoming'></text>
						<text class="tip-text">提示：您有部分绿色积分将于{{userInfo.clear.nextClearDay}}过期，请尽快使用！</text>
					</view>
					<view class='item acea-row row-between-wrapper' v-for="(item,index) in rewardPointList" :key="index">
						<view>
							<view class='state'>{{item.mark}}</view>
							<view>{{item.create_time}}</view>
						</view>
						<view class='num p-color' v-if="item.pm">+{{item.number}}</view>
						<view class='num' v-else>-{{item.number}}</view>
					</view>
					<view class='loadingicon acea-row row-center-wrapper' v-if="rewardPointList.length>0">
						<text class='loading iconfont icon-jiazai' :hidden='loading==false'></text>{{loadTitle}}
					</view>
					<view v-if="rewardPointList.length == 0">
						<emptyPage title="暂无绿色积分记录哦～"></emptyPage>
					</view>
				</view>
        <view v-if="integral_status" class='list' :hidden='current!=1'>
          <view class='item acea-row row-between-wrapper' v-for="(item,index) in rewardPointList" :key="index">
            <view>
              <view class='state'>{{item.mark}}</view>
              <view>{{item.create_time}}</view>
            </view>
            <view class='num p-color' v-if="item.pm">+{{item.number}}</view>
            <view class='num' v-else>-{{item.number}}</view>
          </view>
          <view class='loadingicon acea-row row-center-wrapper' v-if="rewardPointList.length>0">
            <text class='loading iconfont icon-jiazai' :hidden='loading==false'></text>{{loadTitle}}
          </view>
          <view v-if="rewardPointList.length == 0">
            <emptyPage title="暂无绿色积分记录哦～"></emptyPage>
          </view>
        </view>
				<view v-if="integral_status" class='list2' :hidden='current!=2'>
					<navigator class='item acea-row row-between-wrapper' open-type='switchTab' hover-class='none' url='/pages/index/index'>
						<view class='pictrue'>
							<image :src='`${domain}/static/images/score.png`'></image>
						</view>
						<view class='name'>购买商品可获得绿色积分奖励</view>
						<view class='earn'>赚绿色积分</view>
					</navigator>
				</view>
			</view>
		</view>
		<view class="instructions" v-if="showProtocol">
			<view class="setAgCount">
				<i class="icon iconfont icon-cha" @click="showProtocol = false"></i>
				<div class="title">绿色积分说明</div>
				<view class="content">
					<jyf-parser :html="protocol" ref="article" :tag-style="tagStyle"></jyf-parser>
				</view>
			</view>
		</view>
	</view>
</template>

<script>

	import { getRewardInfo, getRewardList, getRewardAgreement } from '@/api/user.js';
	import { mapGetters } from "vuex";
	import emptyPage from '@/components/emptyPage.vue';
	import parser from "@/components/jyf-parser/jyf-parser";
	import { toLogin } from '@/libs/login.js';
	import { HTTP_REQUEST_URL } from '@/config/app';
	const app = getApp();
	export default {
		components: {
			emptyPage,
			"jyf-parser": parser,
		},
		data() {
			return {
				domain: HTTP_REQUEST_URL,
				navList: [{
						'name': '消费积分',
						'icon': 'icon-mingxi'
					},
          {
            'name': '待结算',
            'icon': 'icon-mingxi'
          },
					{
						'name': '如何提升',
						'icon': 'icon-tishengfenzhi'
					}
				],
				current: 0,
				page: 1,
				limit: 10,
				rewardPointList: [],
				userInfo:{},
				loadend: false,
				loading: false,
        category: 'reward',
        type: 'gain',
				loadTitle: '加载更多',
				showProtocol: false,
				protocol: '',
				tagStyle: {
					img: 'width:100%;display:block;',
					video: 'width:100%;'
				},
				integral_status: true,
			};
		},
		computed: mapGetters(['isLogin','viewColor']),
		onLoad() {
			if (this.isLogin) {
				this.getUserInfo();
			} else {
				toLogin()
			}
			this.getAgreement();
		},
		/**
		 * 页面上拉触底事件的处理函数
		 */
		onReachBottom: function () {
			this.getRewardList();
		},
		methods: {
			getUserInfo: function() {
				let that = this;
				getRewardInfo().then(function(res) {
					that.$set(that,'userInfo',res.data);
					that.getRewardList()
				}).catch(res => {
					this.integral_status = false
					return that.$util.Tips({
						title: res
					}, {
						tab: 3,
						url: 1
					});
				});
			},
			/*获取绿色积分说明*/
			getAgreement() {
				let that = this
				getRewardAgreement('sys_reward_rule').then(res => {
					that.protocol = res.data.sys_integral_rule
				})
			},
			/**
			 * 获取绿色积分明细
			 */
			getRewardList: function() {
				let that = this;
				if (that.loading) return;
				if (that.loadend) return;
				that.loading = true;
				that.loadTitle = '';
				getRewardList({
					page: that.page,
					limit: that.limit,
          category: that.category,
          type: that.type
				}).then(function(res) {
					let list = res.data.list,
					loadend = list.length < that.limit;
					that.rewardPointList = that.$util.SplitArray(list, that.rewardPointList);
					that.$set(that,'rewardPointList',that.rewardPointList);
					that.page = that.page + 1;
					that.loading = false;
					that.loadend = loadend;
					that.loadTitle = loadend ? '哼~😕我也是有底线的~' : "加载更多";
				}, function(res) {
					this.loading = false;
					that.loadTitle = '加载更多';
				});
			},
			nav: function(current) {
        console.log(current)
				this.current = current;
        let that = this
        that.loading = false
        that.loadend = false
        that.rewardPointList = []
        if (that.current == 0) {
          that.category = 'reward'
          that.page = 1
          that.type = 'gain'
          that.getRewardList()
        } else if (that.current == 1) {
          that.category = 'reward'
          that.page = 1
          that.type = 'lock'
          that.getRewardList()
        }
			}
		}
	}
</script>

<style scoped lang="scss">
	page{
		background-color: #fff;
	}
	.integral-details .header {
		background-image: url('');
		background-color: var(--view-theme);
		background-repeat: no-repeat;
		background-size: 100% 100%;
		width: 100%;
		// height: 460rpx;
		font-size: 72rpx;
		color: #fff;
		padding: 31rpx 0 30rpx 0;
		// box-sizing: border-box;
		text-align: center;
	}
	.integral-details .header .currentScore {
		font-size: 26rpx;
		color: rgba(255, 255, 255, 0.8);
		text-align: center;
		margin-bottom: 11rpx;
	}
	.currentScore .wenhao{
		display: inline-block;
		width: 32rpx;
		height: 32rpx;
		line-height: 32rpx;
		text-align: center;
		border-radius: 100%;
		background-color: rgba(255,255,255,.8);
		color: var(--view-theme);
		margin-left: 4rpx;
	}
	.wenhao{
		margin-top: -10rpx;
	}
	.integral-details .header .line {
		width: 60rpx;
		height: 3rpx;
		background-color: #fff;
		margin: 20rpx auto 0 auto;
	}

	.integral-details .header .nav {
		font-size: 22rpx;
		color: rgba(255, 255, 255, 0.8);
		flex: 1;
		// 新
		color: black;
		background-color: rgba(240, 240, 240, 0.6);
		backdrop-filter: blur(20px);
		border: 1rpx solid white;
		border-bottom: 0;
		padding: 60rpx 0;
		display: flex;
		flex-direction: row;
		align-items: center;
		border-radius: 12rpx 12rpx 0 0; 
		margin: 60rpx 16px 0 16px ;
		font-size: 22rpx;
	}
	// 新
	.integral-details .header .nav .item:nth-child(2){
		border-right: 1px solid black;
		border-left: 1px solid black;
	}
	.integral-details .header .nav .item {
		width: 33.33%;
		text-align: center;
	}
	.integral-details .header .nav .item .num {
		// color: #fff;
		// font-size: 40rpx;
		margin-bottom: 5rpx;
	}
	.integral-details .wrapper {
		border-radius: 20rpx 20rpx 0 0;
		margin-top: -30rpx;
		background-color: white;
	}
	.integral-details .wrapper .nav {
		// flex: 1;
		// width: 690rpx;
		// border-radius: 20rpx 20rpx 0 0;
		// margin: -96rpx auto 0 auto;
		// background-color: #f7f7f7;
		// height: 96rpx;
		padding: 30rpx 0;
		font-size: 30rpx;
		color: #bbb;
		justify-content: space-evenly;
	}
	.integral-details .wrapper .nav .item {
		text-align: center;
		// width: 33.33%;
		
		// 新
		font-weight: bold;
		border-radius: 40rpx;
		background-color: #f7f7f7;
		padding: 8rpx 12rpx;
		color: #3a3a3a;
	}
	.integral-details .wrapper .nav .item.on {
		background-color: #01d47b;
		// color: var(--view-theme);
		color: white;
		font-weight: bold;
	}
	.p-color {
		color: var(--view-priceColor);
	}
	.integral-details .wrapper .nav .item:nth-of-type(2).on {
		// border-radius: 0 20rpx 0 0;
	}
	.integral-details .wrapper .nav .item .iconfont {
		font-size: 38rpx;
		margin-right: 10rpx;
	}
	.integral-details .wrapper .list {
		background-color: #fff;
		padding: 24rpx 30rpx;
	}
	.integral-details .wrapper .list .tip {
		font-size: 25rpx;
		width: 690rpx;
		border-radius: 50rpx;
		background-color: #fff5e2;
		border: 1px solid #ffeac1;
		color: #c8a86b;
		padding: 10rpx 20rpx 10rpx 55rpx;
		box-sizing: border-box;
		margin-bottom: 24rpx;
		position: relative;
	}
	.integral-details .wrapper .list .tip .iconfont {
		font-size: 35rpx;
		margin-right: 15rpx;
		position: absolute;
		top: 10rpx;
		left: 16rpx;
	}
	.integral-details .wrapper .list .item {
		height: 124rpx;
		border-bottom: 1px solid #eee;
		font-size: 24rpx;
		color: #999;
	}
	.integral-details .wrapper .list .item .state {
		font-size: 28rpx;
		color: #282828;
		margin-bottom: 8rpx;
	}
	.integral-details .wrapper .list .item .num {
		font-size: 36rpx;
	}
	.integral-details .wrapper .list2 {
		background-color: #fff;
		padding: 24rpx 0;
	}
	.integral-details .wrapper .list2 .item {
		background-image: linear-gradient(to right, #fff7e7 0%, #fffdf9 100%);
		width: 690rpx;
		height: 180rpx;
		position: relative;
		border-radius: 10rpx;
		margin: 0 auto 20rpx auto;
		padding: 0 25rpx 0 180rpx;
		box-sizing: border-box;
	}
	.integral-details .wrapper .list2 .item .pictrue {
		width: 90rpx;
		height: 150rpx;
		position: absolute;
		bottom: 0;
		left: 45rpx;
	}
	.integral-details .wrapper .list2 .item .pictrue image {
		width: 100%;
		height: 100%;
	}
	.integral-details .wrapper .list2 .item .name {
		width: 285rpx;
		font-size: 30rpx;
		font-weight: bold;
		color: #c8a86b;
	}
	.integral-details .wrapper .list2 .item .earn {
		font-size: 26rpx;
		color: #c8a86b;
		border: 2rpx solid #c8a86b;
		text-align: center;
		line-height: 52rpx;
		height: 52rpx;
		width: 160rpx;
		border-radius: 50rpx;
	}
	.instructions{
		width: 100%;
		height: 100%;
		position: fixed;
		top: 0;
		left: 0;
		background: rgba(0, 0, 0, .5);
		z-index: 10;
	}
	.instructions .setAgCount {
		background: #fff;
		width: 656rpx;
		height: 458px;
		position: absolute;
		top: 50%;
		left: 50%;
		border-radius: 12rpx;
		-webkit-border-radius: 12rpx;
		padding: 52rpx;
		-webkit-transform: translate(-50%, -50%);
		-moz-transform: translate(-50%, -50%);
		transform: translate(-50%, -50%);
		overflow: hidden;
		.content {
			height: 900rpx;
			overflow-y: scroll;
			/deep/ p {
				font-size: 13px;
				line-height: 22px;
			}
			/deep/ img {
				max-width: 100%;
			}
		}
	}
	.instructions .setAgCount .icon {
		font-size: 42rpx;
		color: #b4b1b4;
		position: absolute;
		top: 15rpx;
		right: 15rpx;
	}
	.instructions .setAgCount .title {
		color: #333;
		font-size: 32rpx;
		text-align: center;
		font-weight: bold;
	}
	.instructions .setAgCount .content {
		margin-top: 32rpx;
		color: #333;
		font-size: 26rpx;
		line-height: 22px;
		text-align: justify;
		text-justify: distribute-all-lines;
		height: 756rpx;
		overflow-y: scroll;
	}
</style>
